package com.wecloud.core.action.bussiness;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.wecloud.core.action.impl.BaseTransCfmAction;
import com.wecloud.core.constant.ErrorMsg;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;
import com.wecloud.core.server.UploadFilesServer;
import com.wecloud.core.utils.RegexValidateUtil;

/**
 * 更新用户信息服务
 * @author qq374
 *
 */
public class UpdateInfoAction extends BaseTransCfmAction{
	@Autowired
	private UploadFilesServer server;
	@Autowired
	private JnlService jnlService;//mysql
	@Override
	public void excute(WeContext context)  throws WeException{
		String jSessionId = context.getString("JSESSIONID");
		String nickName = context.getString("nickName");
		String telphone = context.getString("telphone");
		String email = context.getString("email");
		List list = null;
		try {
			list = (List)server.execute(context).get();
		} catch (UnsupportedEncodingException e) {
			throw new WeException(ErrorMsg.Error_0012);
		} catch (InterruptedException e) {
			throw new WeException(ErrorMsg.Error_0013);
		} catch (ExecutionException e) {
			throw new WeException(ErrorMsg.Error_0013);
		}
		if(CommonUtils.isNotEmpty(email) && !RegexValidateUtil.checkEmail(email)){
			throw new WeException(ErrorMsg.Error_0007);
		}
		if(CommonUtils.isNotEmpty(telphone) && !RegexValidateUtil.checkEmail(telphone)){
			throw new WeException(ErrorMsg.Error_0008);
		}
		//检查手机号码是否已注册
		Map map = new HashMap();
		map.put("telphone", telphone);
		Map resultMap1 = jnlService.queryForObject("user.queryUserByPhone", map);
		if(resultMap1 != null){
			throw new WeException(ErrorMsg.Error_0010);
		}
		//检查邮箱是否已注册
		map.put("email", email);
		Map resultMap2 = jnlService.queryForObject("user.queryUserByEmail", map);
		if(resultMap2 != null){
			throw new WeException(ErrorMsg.Error_0011);
		}
		
	}
}
